const CONST = require('../constant')

module.exports = {
  get CONST () {
    return CONST
  },

  get errors () {
    return {
      SUCCESS: {code: 0, message: `success.`},
      UNLOGINED: {code: 'unlogined', message: `you haven't logged in.`},
      LOGIN_FAIL: {code: 'login_failure', message: `your login failure.`},
      REGISTER_FAIL: {code: 'registration_failure', message: `your registration failure.`},
      WRONG_PWD: {code: 'wrong_password', message: `you supply one wrong password.`},
      UNNECESSARY: {code: 'unnecessary', message: `you are free to do the action but that's unnecessary.`},

      REPEAT_RESOURCES: {code: 'repeat_resource', message: `repeat resources.`},
      UPDATE_FAIL: {code: 'update_failure', message: `updating failure.`},
      INVALID_PARAMS: {code: 'wrong_params', message: `wrong parameters.`},
      NOT_FOUND: {code: 'not_found', message: `resources not found.`},
      NOT_SAME: {code: 'not_same', message: `resources not same.`},

      FORBIDDEN: {code: 'forbidden', message: `you are forbidden to do the action.`},
      NOT_FOR_YOU: {code: 'not_for_you', message: `you are forbidden to operate resource.`},
      PAYMENT_REQUIRED: {code: 'payment_required', message: `payment required.`},
    }
  },

  get validation () {
    return {
      password: {type: 'password', min: 6, max: 16},
      username: {type: 'username', min: 6, max: 16},
      reg_username: {type: 'string', min: 6, max: 16},
      // page offset and size
      offset: { type: 'int', required: true, min: 0 },
      size: { type: 'int', required: true, max: 1000 }
    }
  },

  get resource_validation () {
    return {
      order: {
        create: {
          app_link: {type: 'url', required: true, message: 'app_link should be one url string.'},
          email_addr: {type: 'email', required: true},
          keywords: {type: 'string', required: false, allowEmpty: true},
          need: {type: 'string', required: false, allowEmpty: true},
          target_country: {type: 'string', required: true}
        },

        update: {
          app_link: {type: 'url', required: false, message: 'app_link should be one url string.'},
          email_addr: {type: 'email', required: false},
          keywords: {type: 'string', required: false, allowEmpty: true},
          campaign_manager_name: {type: 'string', required: false, allowEmpty: true, max: 108},
          // status: {type: 'string', enum: this.CONST.order.statusEnum},
          price: {type: 'number', required: false, min: 0},
          need: {type: 'string', required: false, allowEmpty: true},
          target_country: {type: 'string', required: false}
        }
      }
    }
  },

  /**
   * store the request level data
   *
   */
  _hash: {},

  vset (key, value) {
    this._hash[key] = value
  },

  vget (key, value) {
    return this._hash[key]
  },

  get logined () {
    let {username} = this.vget('me') || {}
    return username
  },

  get is_self_admin () {
    let me = this.vget('me')
    let roles = me.roles || []
    return roles.some(x => x === 'administrator')
  }
}
